<?php

/**
 * This is the model class for table "{{message_history}}".
 *
 * The followings are the available columns in table '{{message_history}}':
 * @property integer $id
 * @property integer $sent_user_id
 * @property integer $receive_user_id
 * @property string $content
 * @property string $image
 * @property string $created_time
 */
class Message extends CActiveRecord
{
	public $created_time_from, $created_time_to, $belong_to;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Message the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{message_history}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('sent_user_id, receive_user_id, content', 'required', 'on'=>'insert'),
			array('sent_user_id, receive_user_id', 'numerical', 'integerOnly'=>true),
			array('content', 'length', 'max'=>255),
			array('image', 'length', 'max'=>100),
			array('created_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, sent_user_id, receive_user_id, content, image, created_time,created_time_from,created_time_to', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'sender'=>array(self::BELONGS_TO, 'User', 'sent_user_id'),
			'recp'=>array(self::BELONGS_TO, 'User', 'receive_user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'sent_user_id' => Yii::t('app', 'Sent User'),
			'receive_user_id' => Yii::t('app', 'Receive User'),
			'content' => Yii::t('app', 'Content'),
			'image' => Yii::t('app', 'Image'),
			'created_time' => Yii::t('app', 'Time'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$c=new CDbCriteria;

		if ($this->created_time_from) {
			$from = date('Y-m-d',strtotime($this->created_time_from));
			$c->addCondition("created_time >= '$from'");
		}

		if ($this->created_time_to) {
			$to = date('Y-m-d 23:59:59',strtotime($this->created_time_to));
			$c->addCondition("created_time <= '$to'");
		}
		
		if ($this->belong_to) {
			$c->addCondition("sent_user_id={$this->belong_to} OR receive_user_id={$this->belong_to}");
		}
		$c->compare('sent_user_id', $this->sent_user_id);
		$c->order = 'created_time DESC';
		// $c->select = 't.id, t.sent_user_id, MAX(created_time) AS created_time';
		// $c->group = 'sent_user_id';

		return new CActiveDataProvider($this, array(
			'criteria'=>$c,
		));
	}
	
	public function getSearchSummary(){
		$c=new CDbCriteria;
		if ($this->created_time_from) {
			$from = date('Y-m-d',strtotime($this->created_time_from));
			$c->addCondition("t.created_time >= '$from'");
		}

		if ($this->created_time_to) {
			$to = date('Y-m-d 23:59:59',strtotime($this->created_time_to));
			$c->addCondition("t.created_time <= '$to'");
		}
		
		// user send
		$c1 = clone $c;
		$c1->with = 'sender';
		$c1->select = 'DISTINCT t.sent_user_id';
		$c1->compare('sender.role_id', Role::ROLE_USER);
		
		//operator send
		$c2 = clone $c;
		$c2->with = 'sender';
		$c2->select = 'DISTINCT t.sent_user_id';
		$c2->compare('sender.role_id', Role::ROLE_OPERATOR);
		
		// user receive
		$c3 = clone $c;
		$c3->with = 'recp';
		$c3->select = 'DISTINCT t.receive_user_id';
		$c3->compare('recp.role_id', Role::ROLE_USER);
		
		// operator receive
		$c4 = clone $c;
		$c4->with = 'recp';
		$c4->select = 'DISTINCT t.receive_user_id';
		$c4->compare('recp.role_id', Role::ROLE_OPERATOR);
		
		$res = array(
			'user_send' => $this->count($c1),
			'opr_send' => $this->count($c2),
			'user_recv' => $this->count($c3),
			'opr_recv' => $this->count($c4),
		);
		return $res;
	}
}